package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FileEntity;
import com.ruoyi.system.domain.vo.SysFileShareReq;
import com.ruoyi.system.domain.vo.SysFileShareResp;
import com.ruoyi.system.enums.LikeRecordEnum;
import com.ruoyi.system.service.FileService;
import com.ruoyi.system.service.ISysLikeRecordService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysFileShareMapper;
import com.ruoyi.system.domain.SysFileShare;
import com.ruoyi.system.service.ISysFileShareService;

import javax.servlet.http.HttpServletResponse;

/**
 * 资源共享Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-02
 */
@Service
public class SysFileShareServiceImpl implements ISysFileShareService 
{
    @Autowired
    private SysFileShareMapper sysFileShareMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysLikeRecordService likeRecordService;
    @Value("${url.backend}")
    private String backendUrl;

    /**
     * 查询资源共享
     * 
     * @param id 资源共享主键
     * @return 资源共享
     */
    @Override
    public SysFileShare selectSysFileShareById(Long id)
    {
        return sysFileShareMapper.selectSysFileShareById(id);
    }

    /**
     * 查询资源共享列表
     * 
     * @param sysFileShare 资源共享
     * @return 资源共享
     */
    @Override
    public List<SysFileShareResp> selectSysFileShareList(SysFileShareReq sysFileShare)
    {
        if (Objects.nonNull(sysFileShare.getIsSelf()) && sysFileShare.getIsSelf()) {
            sysFileShare.setUserId(SecurityUtils.getUserId());
        }
        return sysFileShareMapper.selectSysFileShareList(sysFileShare).stream().map(item -> {
            FileEntity file = fileService.getById(item.getFileId());
            SysFileShareResp resp = BeanUtil.copyProperties(item, SysFileShareResp.class);
            SysUser sysUser = userService.selectUserById(item.getUserId());
            resp.setFileSize(file.getTotalSize());
            resp.setFilename(file.getFilename());
            resp.setFileType(file.getFileType());
            resp.setNickName(sysUser.getNickName());
            resp.setLikeNum(likeRecordService.selectSysLikeRecordByTargetIdAndType(item.getId(), LikeRecordEnum.RESOURCE.getKey()));
            return resp;
        }).collect(Collectors.toList());
    }

    /**
     * 新增资源共享
     * 
     * @param sysFileShare 资源共享
     * @return 结果
     */
    @Override
    public int insertSysFileShare(SysFileShare sysFileShare)
    {
        sysFileShare.setCreateTime(DateUtils.getNowDate());
        sysFileShare.setUserId(SecurityUtils.getUserId());
        sysFileShare.setDownloadNum(0);
            return sysFileShareMapper.insertSysFileShare(sysFileShare);
    }

    /**
     * 修改资源共享
     * 
     * @param sysFileShare 资源共享
     * @return 结果
     */
    @Override
    public int updateSysFileShare(SysFileShare sysFileShare)
    {
        sysFileShare.setUpdateTime(DateUtils.getNowDate());
        return sysFileShareMapper.updateSysFileShare(sysFileShare);
    }

    /**
     * 批量删除资源共享
     * 
     * @param ids 需要删除的资源共享主键
     * @return 结果
     */
    @Override
    public int deleteSysFileShareByIds(Long[] ids)
    {
        return sysFileShareMapper.deleteSysFileShareByIds(ids);
    }

    /**
     * 删除资源共享信息
     * 
     * @param id 资源共享主键
     * @return 结果
     */
    @Override
    public int deleteSysFileShareById(Long id)
    {
        return sysFileShareMapper.deleteSysFileShareById(id);
    }

    @Override
    public String frontDownload(Long id) {
        SysFileShare sysFileShare = selectSysFileShareById(id);
        sysFileShare.setDownloadNum(sysFileShare.getDownloadNum() + 1);
        sysFileShareMapper.updateSysFileShare(sysFileShare);
        return backendUrl + "/oss/download/" + sysFileShare.getFileId();
    }
}
